package algorithm.array;

/**
 * @author bingo
 * @Description
 * @Date 2018/6/1
 */
public class MyArray<E> {

    private int size;

    private E[] data;

    public MyArray(int capacity) {
        size=0;
        data = (E[]) new Object[capacity];
    }

    public int getCapacity() {
        return data.length;
    }


    public int getSize() {
        return size;
    }

    public E[] getData() {
        return data;
    }

    /**
     * 增加元素
     * @param index 索引
     * @param e    元素
     * */

    public void insert(int index, E e){
        if(size==getCapacity()){
            this.resize(data.length*2);
        }
        for(int i=size-1; i>=index; i--){
            data[i+1] = data[i];
        }
        data[index]=e;
        size++;

    }
    /**
     * 插入第一的位置
     * @@param e 元素
     *
     * */
    public void insertFirst(E e){
        insert(0,e);
    }

    /**插入最后的位置**/
    public void insertLast(E e){

        insert(size,e);
    }


    /** 删除数组中的元素
     *  @param index  索引
     *
     */
    public E  remove(int index){
         E e = data[index];
        for(int i=index+1;i<size;i++){
           data[i-1] = data[i];
        }
        size--;
        data[size] = null;
        if(size==getCapacity()/2){
            resize(getCapacity()/2);
        }
        return  e;
    }
  /**
   * 删除数组头元素
   * */

    public E removeFirst(){
      return    remove(0);
    }
    /**
     * 删除数组末尾元素
     * */

    public E removeLast(){
       return remove(size-1);
    }

/**
 *    更新值
 * */
    public void set(int index,E e) {

        if(!checkLegal(index)){
            throw new IllegalArgumentException();
        }
        data[index] = e;
    }

    public int find(E e){

        for(int i=0;i<data.length;i++){
            if(data[i]==e){
                return  i;
            }
        }

        return  -1;
    }


    public E  get(int index){

        if(!checkLegal(index)){
            throw  new IllegalArgumentException();
        }
        return  data[index];
    }
    /**
     *  返回最后一个元素
     * */
    public E getLast(){

       return get(size-1);
    }

    public E getFirst(){

        return get(0);
    }

    public  boolean isEmpty(){
        return  size==0;
    }

    /**
     *
     * 检查索引是否合法
     * @param index  索引
     * */

    public boolean checkLegal(int index){
       return index >= 0 && index < size;
    }

    /**
     *
     *  重新开辟新的数组空间
     *
     *
     * */
    public void resize(int length){
        E[]  newData = (E[]) new Object[length];
        for(int i=0;i<size;i++){
           newData[i] = data[i];
        }
        data = newData;
    }

    @Override
    public String toString(){
        StringBuilder  result = new StringBuilder();
        result.append(String.format("Array:size=%d,capacity=%d",size,getCapacity()));
        result.append('[');
        for(int i=0;i<size;i++){
          result.append(data[i]);
          if(i!=size-1){
            result.append(',');
          }
        }
        result.append("]");
        return  result.toString();
    }


    public static void main(String[] args) {
        MyArray myArray = new MyArray(6);
        myArray.insert(0,1);
        myArray.insert(0,2);
        myArray.insert(1,3);
        myArray.insertFirst(10);
        myArray.insertLast(20);
        myArray.insertLast(30);
        System.out.println(myArray.toString());

       myArray.removeLast();
        System.out.println(myArray.toString());

        myArray.removeFirst();
        System.out.println(myArray.toString());

        myArray.removeFirst();
        System.out.println(myArray.toString());
    }

}
